package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.Bean.Student;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Register extends AppCompatActivity {

    private EditText mEtPhone;
    private EditText mEtPassWord;
    private EditText mEtRePassWord;
    private Button mBtnSubmit;
    private EditText mEtName;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initEvent();
        handler = new Handler();
    }

    private void initEvent() {
        mBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String phone = mEtPhone.getText().toString().trim();
                        String password = mEtPassWord.getText().toString().trim();
                        String name = mEtName.getText().toString().trim();

                        OkHttpClient okHttpClient =new OkHttpClient();

                        RequestBody requestBody = new FormBody.Builder()
                                .add("telephone",phone)
                                .add("password",password)
                                .add("name",name)
                                .add("email","2723613983@qq.com")
                                .add("department","信息学院")
                                .add("major","移动应用开发")
                                .add("role","学生")
                                .build();

                        Request request = new Request.Builder()
                                .url("http://anli-wechat.boruiman.com/freshmenapp/user/regist")
                                .post(requestBody)
                                .build();
                        okHttpClient.newCall(request).enqueue(new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                Toast.makeText(Register.this, "网络请求失败", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onResponse(Call call, final Response response) throws IOException {
                                if (!response.isSuccessful()){
                                    Toast.makeText(Register.this, "请求失败，服务器异常", Toast.LENGTH_SHORT).show();
                                }
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        Gson gson = new Gson();
                                        Student student = null;
                                        try {
                                            student = gson.fromJson(response.body().string(),Student.class);
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                        Toast.makeText(Register.this, "注册成功"+student.getMajor(), Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        });
                    }
                }).start();
            }
        });
    }

    private void initView() {
        mEtPhone = findViewById(R.id.et_phone);
        mEtPassWord = findViewById(R.id.et_password);
        mEtRePassWord = findViewById(R.id.et_repassword);
        mBtnSubmit = findViewById(R.id.btn_submit);
    }
}